package com.bootdo.shop.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.bootdo.common.dao.CommonImageDao;
import com.bootdo.common.domain.CommonImageDO;
import com.bootdo.shop.dao.ProductCategoryDao;
import com.bootdo.shop.dao.ProductDao;
import com.bootdo.shop.domain.ProductCategoryDO;
import com.bootdo.shop.domain.ProductDO;
import com.bootdo.shop.service.ProductService;



@Service
public class ProductServiceImpl implements ProductService {
	@Autowired
	private ProductDao productDao;
	
	@Autowired
	private ProductCategoryDao productCategoryDao;
	@Autowired
	private CommonImageDao commonImageDao;
	@Override
	public ProductDO get(Long productId){
		return productDao.get(productId);
	}
	
	@Override
	public List<ProductDO> list(Map<String, Object> map){
		return productDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return productDao.count(map);
	}
	
	@Override
	public int save(ProductDO product){
		
		product.setCreateTime(new Date());
		int i = productDao.save(product);
			//添加类型和产品的关系表
			ProductCategoryDO pc = new ProductCategoryDO();
			pc.setCategoryId(product.getCategoryId());
			pc.setProductId(product.getProductId());
			pc.setCreateTime(new Date());
			int c =  productCategoryDao.save(pc);
			//添加多图
			if(StringUtils.isNotEmpty(product.getProimgs())) {
				List<String> imglist = Arrays.asList(product.getProimgs().split(","));
				for (String imgurl : imglist) {
					CommonImageDO img = new CommonImageDO();
					img.setCreateTime(new Date());
					img.setCommonId(product.getProductId());
					img.setImageUrl(imgurl);
					img.setType("product");
					commonImageDao.save(img);
				}
			}
		
		return i+c;
	}
	
	@Override
	public int update(ProductDO product){
		//添加类型和产品的关系表
		ProductCategoryDO pc = productCategoryDao.getByProductId(product.getProductId());
		pc.setCategoryId(product.getCategoryId());
		pc.setProductId(product.getProductId());
		pc.setCreateTime(new Date());
		int c =  productCategoryDao.update(pc);
		return productDao.update(product);
	}
	
	@Override
	public int remove(Long productId){
		return productDao.remove(productId);
	}
	
	@Override
	public int batchRemove(Long[] productIds){
		return productDao.batchRemove(productIds);
	}

	@Override
	public List<ProductDO> hotProductList() {
		
		
		
		return productDao.hotProductList();
	}
	
}
